package com.wnhz.work.jy.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HDate {

	/**
	 * 获取当前日期是星期几<br>
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	/**
	 * 两个日期做减法，返回相差天数
	 * 
	 * @throws ParseException
	 * @throws ParseException
	 */
	public static long datesub(Date date1, Date date2) throws ParseException {
		@SuppressWarnings("unused")
		long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()
				- date2.getTime() : date2.getTime() - date1.getTime();
		// 日期相减得到相差的日期
		long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0 ? (date1
				.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000)
				: (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
		return day + 1;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(8);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	/**
	 * yyyy-MM-dd HH:mm:ss 将字符串转换成Date类型
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */

	public static Date StringDate(String str) throws ParseException {
		SimpleDateFormat sdm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdm.parse(str);
	}

	public static Date toDate(String str) throws ParseException {
		SimpleDateFormat sdm = new SimpleDateFormat("yyyy-MM-dd");
		return sdm.parse(str);
	}

	/**
	 * yyyy-MM-dd 将字符串转换成Date类型
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */

	public static Date StringDateShort(String str) throws ParseException {
		SimpleDateFormat sdm = new SimpleDateFormat("yyyy-MM-dd");
		return sdm.parse(str);
	}

	/**
	 * date类型转换成long类型
	 */

	public static long getDateLong(Date d) {
		return d.getTime();
	}

	/**
	 * long类型转换成date类型
	 * 
	 * @param l
	 * @return
	 */

	public static Date getLongDate(long l) {
		return new Date(l);
	}

	/**
	 * 获取现在时间
	 * 
	 * @return返回短时间格式 yyyy-MM-dd
	 */
	public static Date getNowDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(8);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回短时间字符串格式yyyy-MM-dd
	 */
	public static String getStringDateShort() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回短时间字符串格式yyyy-MM-dd
	 */
	public static String StringDateShort(Date date) {

		// SimpleDateFormat formatter = ;
		// String dateString =
		return new SimpleDateFormat("MM-dd HH:mm").format(date);
	}

	/**
	 * 获取时间 小时:分;秒 HH:mm:ss
	 * 
	 * @return
	 */
	public static String getTimeShort() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		Date currentTime = new Date();
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLong(Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStr(Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 得到现在时间
	 * 
	 * @return
	 */
	public static Date getNow() {
		Date currentTime = new Date();
		return currentTime;
	}

	/**
	 * 提取一个月中的最后一天
	 * 
	 * @param day
	 * @return
	 */
	public static Date getLastDate(long day) {
		Date date = new Date();
		long date_3_hm = date.getTime() - 3600000 * 34 * day;
		Date date_3_hm_date = new Date(date_3_hm);
		return date_3_hm_date;
	}

	/**
	 * 得到现在时间
	 * 
	 * @return 字符串 yyyyMMdd HHmmss
	 */
	public static String getStringToday() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 得到现在小时
	 */
	public static String getHour() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String hour;
		hour = dateString.substring(11, 13);
		return hour;
	}

	/**
	 * 得到现在分钟
	 * 
	 * @return
	 */
	public static String getTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		String min;
		min = dateString.substring(14, 16);
		return min;
	}

	/**
	 * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。
	 * 
	 * @param sformat
	 *            yyyyMMddhhmmss
	 * @return
	 */
	public static String getUserDate(String sformat) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(sformat);
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式，返回字符型的分钟
	 */
	public static String getTwoHour(String st1, String st2) {
		String[] kk = null;
		String[] jj = null;
		kk = st1.split(":");
		jj = st2.split(":");
		if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
			return "0";
		else {
			double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])
					/ 60;
			double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])
					/ 60;
			if ((y - u) > 0)
				return y - u + "";
			else
				return "0";
		}
	}

	/**
	 * 得到二个日期间的间隔天数
	 */
	public static String getTwoDay(String sj1, String sj2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try {
			Date date = myFormatter.parse(sj1);
			Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return "";
		}
		return day + "";
	}

	public static String getDateStr() {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		return myFormatter.format(new Date());
	}
	
	/**
	 * 得到年月日，时分秒格式的时间
	 * @return
	 */
	public static String getAllDateStr(Date date){
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return myFormatter.format(date);
	}

    public static String getAllDateStrByLong(long date){
        return getAllDateStr(new Date(date));

    }


    public static String getUserChatShowTime(long time){
        String reuslt="";
        Date date = new Date(time);

        long uTime = time / 1000;
        long nowTime = System.currentTimeMillis() / 1000;
        long cTime = nowTime-uTime;

        SimpleDateFormat sdf = new SimpleDateFormat();
        /**如果是一天之前,就只返回小时*/
        if(cTime <86400 ){
            sdf.applyPattern("HH:mm:ss");
            reuslt = sdf.format(date);
        }
        /**如果是一天之前，就返回日期*/
        else{
            sdf.applyPattern("yyyy-MM-dd HH:mm");
            reuslt = sdf.format(date);
        }

        return reuslt;
    }


    public static String getUShowTime(long time){

        String reuslt="";
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        try {

            long uTime = time / 1000;
            long nowTime = System.currentTimeMillis() / 1000;
            long cTime = nowTime - uTime;
            if (cTime < 60) {
                reuslt = "刚刚";
            } else if (cTime < 3600) {
                reuslt = (cTime / 60) + "分钟前";
            } else if (cTime < 86400) {
                reuslt = (cTime / 3600) + "小时前";
            } else if (cTime < 2592000) {
                reuslt = (cTime / 86400) + "天前";
            } else {
                sdf.applyPattern("yyyy-MM-dd");
                reuslt = sdf.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

        return reuslt;
    }


    public static String getYearMonethData(String time){
        String resultTime = "";
        if (time == null)
            return resultTime;
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        try{
            Date date = sdf.parse(time);
            resultTime = sdf.format(date);
        }
        catch (Exception e){

        }


        return resultTime;
    }


	/**
	 * 得到友好时间
	 * 
	 * @param time
	 *            yyyy-MM-dd HH:mm:ss
	 */
	public static String getUShowTime(String time) {
		String resultTime = "";
		if (TextUtils.isEmpty(time))
			return resultTime;

		SimpleDateFormat sdf = new SimpleDateFormat();
		SimpleDateFormat sdfcompare = (SimpleDateFormat) sdf.clone();
		sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
		sdfcompare.applyPattern("yyyy-MM-dd");
		String sss = sdf.format(System.currentTimeMillis());
		try {
			Date date = sdf.parse(time);
			Date date1 = sdfcompare.parse(sss);
			long d1 = date1.getTime() / 1000;
			long uTime = date.getTime() / 1000;
			long nowTime = System.currentTimeMillis() / 1000;
			//得到当前年月日
			String currentDate = sdfcompare.format(System.currentTimeMillis());
			
			String standard_time = currentDate +" 12:00:00";
			//得到传过来值得年月日
			String targetDate = sdfcompare.format(date);
			//如果是相同的一天
			String string;
			System.out.println("currentDate"+currentDate+"  " +"targetDate" + targetDate);
			if(currentDate.equals(targetDate)){
				System.out.println(standard_time);
				/*if (sdf.parse(standard_time).getTime()/1000 > uTime) {
					string = "上午";
				}else {
					string = "下午";
				}*/
				sdf.applyPattern(" HH:mm ");
				resultTime = sdf.format(date);
				System.out.println(""+sdf.format(date));
			}
			else{

				long cTime = nowTime - uTime;
				int onedaytime = 86400;
				if(d1 > uTime && uTime > d1 - onedaytime){
					sdf.applyPattern(" HH:mm ");
					resultTime = "昨天 "+ sdf.format(date);
				}
				/*else if (d1 - onedaytime >uTime && uTime > d1 - onedaytime * 2){
					resultTime = "两天前 ";
				}
				else if (d1 - onedaytime * 2 >uTime && uTime > d1 - onedaytime * 3){
					resultTime = "三天前 ";
				}
				else if (d1 - onedaytime * 3 >uTime && uTime > d1 - onedaytime * 4){
					resultTime = "四天前 ";
				}
				else if (d1 - onedaytime * 4 >uTime && uTime > d1 - onedaytime * 5){
					resultTime = "五天前 ";
				}
				else if (d1 - onedaytime * 5 >uTime && uTime > d1 - onedaytime * 6){
					resultTime = "六天前 ";
				}*/
				else {
					sdf.applyPattern("yyyy-MM-dd HH:mm");
					resultTime = sdf.format(date);
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();

		}
		return resultTime;
	}

	public static Date getAfterDate(Date date,int n) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(date.getTime());
		c.add(Calendar.DATE, n);
		Date afterData= new Date(c.getTimeInMillis());
		return afterData;
	}

}